#include "CMusic.h"

CMusic* CMusic::_sInstance = 0;

CMusic* CMusic::GetInstance()
{
	if (_sInstance == NULL)
		_sInstance = new CMusic;

	return _sInstance;
}

CMusic::CMusic()
{
	_soundFile = NULL;
}

CMusic::~CMusic()
{
	delete _soundFile;

	if (this->_sInstance != NULL)
		delete _sInstance;
}

int CMusic::InitSound(HWND hwnd)
{
	//initialize DirectSound
	if (!Init_DirectSound(hwnd))
	{
		MessageBox(hwnd, "Error initializing DirectSound", "Error", MB_OK);
		return 0;
	}
	
	TurnOn(true);
	//load bounce wave file
	/*backmenu = LoadSound("Resource/sound/backmenu.wav");
	backlevel1 = LoadSound("Resource/sound/backlevel1.wav");
	backlevel2 = LoadSound("Resource/sound/backlevel2.wav");
	backlevel3 = LoadSound("Resource/sound/backlevel3.wav");
	BrickBroken = LoadSound("Resource/sound/BrickBroken.wav");
	BrickNoBroken = LoadSound("Resource/sound/BrickNoBroken.wav");
	coin = LoadSound("Resource/sound/coin.wav");
	death = LoadSound("Resource/sound/death.wav");
	fireball = LoadSound("Resource/sound/fireball.wav");
	gameover = LoadSound("Resource/sound/gameover.wav");
	grow = LoadSound("Resource/sound/grow.wav");
	jump = LoadSound("Resource/sound/jump.wav");
	killEnemy = LoadSound("Resource/sound/killEnemy.wav");
	Life = LoadSound("Resource/sound/Life.wav");
	reduce = LoadSound("Resource/sound/reduce.wav");
	MenuChange = LoadSound("Resource/sound/MenuChange.wav");
	pause = LoadSound("Resource/sound/pause.wav");
	select = LoadSound("Resource/sound/select.wav");
	WinStage = LoadSound("Resource/sound/WinStage.wav");
	BulletExplosion = LoadSound("Resource/sound/BulletExplosion.wav");*/

	// return success
	return 1;
}

int CMusic::Init_DirectSound(HWND hwnd)
{
	HRESULT result;

	//create DirectSound manager object
	dsound = new CSoundManager();

	//initialize DirectSound
	result = dsound->Initialize(hwnd, DSSCL_PRIORITY);
	if (result != DS_OK)
		return 0;

	//set the primary buffer format
	result = dsound->SetPrimaryBufferFormat(2, 22050, 16);
	if (result != DS_OK)
		return 0;

	//return success
	return 1;
}

CSound* CMusic::LoadSound(char *filename)
{
	HRESULT result;

	//create local reference to wave data
	CSound *wave;

	//attempt to load the wave file
	result = dsound->Create(&wave, filename);
	if (result != DS_OK)
		return NULL;

	//return the wave
	return wave;
}

void CMusic::TurnOff(bool turn)
{
	
	_turnOn = turn;
}
void CMusic::TurnOn(bool turn)
{
	_turnOn = turn;
}